package  
{
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.geom.Point;
  import mge.Ease;
  import mge.Timer;
	
	/**
   * ...
   * @author ivan
   */
  public class FuncyButton extends MovieClip 
  {
    private const NUM_TIMERS:int = 5;
  
    public var speed:Number = 3000;
    public var speedVar:Number = 1500;
    
    [Inspectable]
    public var sector:Number;
  
    public function FuncyButton() 
    {
      super();
      stop();
      
      mouseEnabled = true;
      buttonMode = true;
      
      orbitCenter = new Point(x, y);
      
      for (var i:int = 0; i < NUM_TIMERS * 3; i++)
      {
        var t:Timer = new Timer(speed + Math.random() * speedVar);
        t.start(true);
        t.setDelta(Math.random());
        if (i % 3 == 0) timersX.push(t);
        if (i % 3 == 1) timersY.push(t);
        if (i % 3 == 2) timersRot.push(t);
      }
      
      addEventListener(Event.ENTER_FRAME, update);
      /*
      addEventListener(MouseEvent.ROLL_OVER, onRollOver);
      addEventListener(MouseEvent.ROLL_OUT, onRollOut);*/
    }
    
    /*
    private function onRollOver(e:MouseEvent):void
    {
      gotoAndStop("over");
    }
    
    private function onRollOut(e:MouseEvent):void
    {
      gotoAndStop("up");
    }*/
    
    private function update(e:Event = null):void
    {
      var x_desp:Number = getAverage(timersX) * 6;
      var y_desp:Number = getAverage(timersY) * 6;
      var rot:Number = getAverage(timersRot) * 16;
      
      x = orbitCenter.x + x_desp;
      y = orbitCenter.y + y_desp;
      
      rotation = rot;
    }
    
    private function getAverage(v:Vector.<Timer>):Number
    {
      var ans:Number = 0;
      for (var i:int = 0; i < NUM_TIMERS; i++)
        ans += v[i].getDelta(new Ease(Ease.sin2Pi_00));
      ans /= NUM_TIMERS;
      return ans;
    }
    
    public var timersX:Vector.<Timer> = new Vector.<Timer>();
    public var timersY:Vector.<Timer> = new Vector.<Timer>();
    public var timersRot:Vector.<Timer> = new Vector.<Timer>();
    private var orbitCenter:Point;
    
    override public function set enabled (value:Boolean) : void { gotoAndStop(value ? "_up" : "_disabled"); super.enabled = value}
    

    
  }

}
